/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package domain;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jeroen
 */
public class TCPServerThread extends Thread {

    private static ServerSocket servSock;
    private Properties properties;

    public TCPServerThread() {
        properties = new Properties();
        try {
            properties.load(new FileInputStream("Ports.properties"));
        } catch (FileNotFoundException ex) {
            System.err.println(ex);
            System.exit(1);
        } catch (IOException ex) {
            System.err.println(ex);
            System.exit(1);
        }
        try {
            servSock = new ServerSocket(Integer.parseInt(properties.getProperty("Poort")));
        } catch (IOException e) {
            System.out.println("Unable to attach to port!");
            System.exit(1);
        }

    }

    public void run() {
        while (true) {
            Socket link = null;                        //Step 2.
            try {
                link = servSock.accept();

                BufferedReader in =
                        new BufferedReader(
                        new InputStreamReader(
                        link.getInputStream())); //Step 3.
                PrintWriter out = new PrintWriter(
                        link.getOutputStream(), true);

                String message = in.readLine();
                out.println("Message: " + message);

                try {
                    sleep((long) (/*Math.random() * */5000));
                } catch (InterruptedException e) {
                }
            } catch (IOException ex) {
                Logger.getLogger(TCPServerThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
//        socket.close();
    }
}